package com.bzkj.web.controller.ncc;

import com.bzkj.basicinfo.domain.ncc.NccBasicSkuInfo;
import com.bzkj.basicinfo.domain.ncc.dto.NccBasicSkuInfoDto;
import com.bzkj.basicinfo.domain.ncc.vo.SkuInfoExportVo;
import com.bzkj.basicinfo.service.ncc.NccBasicSkuInfoService;
import com.bzkj.common.annotation.LogC;
import com.bzkj.common.core.controller.BaseController;
import com.bzkj.common.core.domain.AjaxResult;
import com.bzkj.common.core.page.TableDataInfo;
import com.bzkj.common.enums.BusinessType;
import com.bzkj.common.utils.poi.CsvExportUtils;
import com.bzkj.common.utils.poi.ExcelPDFUtil;
import com.bzkj.common.utils.poi.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author wangzhichao
 * @ClassName NccBasicSkuInfoController
 * @Date 2024/12/09 14:03:00
 * @description NCC商品信息表
 */
@Api(tags = "商品信息")
@RestController
@RequestMapping("/ncc/basic/skuInfo")
public class NccBasicSkuInfoController extends BaseController
{
    @Autowired
    private NccBasicSkuInfoService nccBasicSkuInfoService;

    /**
     * 根据条件查询ncc商品信息
     *
     * @Author wangzhichao
     * @Date 2024/12/10 16:24:47
     * @Param dto
     */
    @ApiOperation("查询接口")
    @PreAuthorize("@ss.hasPermi('basic:nccBasic:query')")
    @GetMapping("findNccBasicSkuInfoData")
    public TableDataInfo findNccBasicSkuInfoData(NccBasicSkuInfoDto dto)
    {
        startPage();
        return getDataTable(nccBasicSkuInfoService.findNccBasicSkuInfoData(dto));
    }

    /**
     * 根据pk_material修改ncc商品信息
     *
     * @Author wangzhichao
     * @Date 2024/12/11 13:19:44
     * @Param entity
     */
    @ApiOperation("修改接口")
    @PreAuthorize("@ss.hasPermi('basic:nccBasic:edit')")
    @PutMapping("nccBasicSkuInfoUpdate")
    @LogC(title = "ncc商品信息", businessType = BusinessType.UPDATE,
            tableNames = "ncc_basic_sku_info", pkFieldNames = "pk_material", content = "' 商品名称：'+#entity.name")
    public AjaxResult nccBasicSkuInfoUpdate(@RequestBody NccBasicSkuInfo entity)
    {
        return nccBasicSkuInfoService.nccBasicSkuInfoUpdate(entity);
    }

    /**
     * 新增ncc商品信息
     *
     * @Author wangzhichao
     * @Date 2024/12/11 13:32:08
     * @Param entity
     */
    @ApiOperation("新增接口")
    @PreAuthorize("@ss.hasPermi('basic:nccBasic:add')")
    @PostMapping("nccBasicSkuInfoSave")
    @LogC(title = "ncc商品信息", businessType = BusinessType.INSERT,
            tableNames = "ncc_basic_sku_info", pkFieldNames = "id", content = "' 商品名称：'+#entity.name")
    public AjaxResult nccBasicSkuInfoSave(@RequestBody NccBasicSkuInfo entity)
    {
        return nccBasicSkuInfoService.nccBasicSkuInfoSave(entity);
    }

    /**
     * 根据pkMaterial修改商品状态
     *
     * @Author wangzhichao
     * @Date 2024/12/11 14:17:12
     * @Param status
     * @Param pk_material
     */
    @ApiOperation("修改状态接口")
    @PreAuthorize("@ss.hasPermi('basic:nccBasic:stateEdit')")
    @PutMapping("nccBasicSkuInfoEnableStatusUpdate")
    @LogC(title = "ncc商品信息", businessType = BusinessType.UPDATE,
            tableNames = "ncc_basic_sku_info", pkFieldNames = "pk_material", content = "' 商品id：'+#pkMaterial")
    public AjaxResult nccBasicSkuInfoEnableStatusUpdate(@RequestParam String status, @RequestParam String pkMaterial)
    {
        return nccBasicSkuInfoService.nccBasicSkuInfoEnableStatusUpdate(status, pkMaterial);
    }

    /**
     * 模糊查询商品名称列表
     *
     * @param name
     * @return
     */
    @PreAuthorize("@ss.hasPermi('basic:nccBasic:query')")
    @GetMapping("findNccBasicSkuInfoNameList")
    public List<String> findNccBasicSkuInfoNameList(String name)
    {
        return nccBasicSkuInfoService.findNccBasicSkuInfoNameList(name);
    }

    /**
     * 定时任务
     * 定时获取商品数据
     * 获取当天数据
     *
     * @return
     */
    @GetMapping("jobMethodExecute")
    public AjaxResult jobMethodExecute()
    {
        nccBasicSkuInfoService.jobMethodExecute(true);
        return AjaxResult.success();
    }

    /**
     * 定时任务
     * 根据效期自动冻结
     *
     * @return
     */
    @GetMapping("jobExpMethodExecute")
    public void jobExpMethodExecute() {
        nccBasicSkuInfoService.jobExpMethodExecute();
    }

    /**
     * 根据pk值创建动态表
     *
     * @param tableName
     * @return
     */
    @PostMapping("createTableXXXSku")
    public AjaxResult createTableXXXSku(@RequestParam("tableName") String tableName)
    {
        return nccBasicSkuInfoService.nccBasicSkuInfoCreateTableXXXSku(tableName);
    }

    /**
     * 商品信息导出
     *
     * @param dto
     * @param response
     */
    @ApiOperation("导出excel")
    @PreAuthorize("@ss.hasPermi('basic:nccBasic:export')")
    @PostMapping("skuInfoExport")
    @LogC(title = "ncc商品信息", businessType = BusinessType.EXPORT)
    public void skuInfoExport(NccBasicSkuInfoDto dto, HttpServletResponse response)
    {
        List<NccBasicSkuInfo> list = nccBasicSkuInfoService.findNccBasicSkuInfoData(dto);
        List<SkuInfoExportVo> collect = list.parallelStream().map(item -> {
            SkuInfoExportVo vo = new SkuInfoExportVo();
            BeanUtils.copyProperties(item, vo);
            return vo;
        }).collect(Collectors.toList());

        ExcelUtil<SkuInfoExportVo> util = new ExcelUtil<>(SkuInfoExportVo.class);
        util.exportExcel(response, collect, "ncc商品信息");
    }

    @ApiOperation("导出pdf")
    @PreAuthorize("@ss.hasPermi('basic:nccBasic:export')")
    @PostMapping("skuInfoExportPdf")
    @LogC(title = "ncc商品信息", businessType = BusinessType.EXPORTPDF)
    public AjaxResult skuInfoExportPdf(NccBasicSkuInfoDto dto, HttpServletResponse response) {
        List<NccBasicSkuInfo> list = nccBasicSkuInfoService.findNccBasicSkuInfoData(dto);
        List<SkuInfoExportVo> collect = list.parallelStream().map(item -> {
            SkuInfoExportVo vo = new SkuInfoExportVo();
            BeanUtils.copyProperties(item, vo);
            return vo;
        }).collect(Collectors.toList());

        ExcelPDFUtil<SkuInfoExportVo> util = new ExcelPDFUtil<>(SkuInfoExportVo.class);
        util.exportPDF(response, collect, "ncc商品信息", "ncc商品信息", getUsername(), new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()), "全量");
        return AjaxResult.success();
    }
    @ApiOperation("导出csv")
    @PreAuthorize("@ss.hasPermi('basic:nccBasic:export')")
    @PostMapping("skuInfoExportCsv")
    @LogC(title = "ncc商品信息", businessType = BusinessType.EXPORTCSV)
    public AjaxResult skuInfoExportCsv(NccBasicSkuInfoDto dto, HttpServletResponse response) {
        List<NccBasicSkuInfo> list = nccBasicSkuInfoService.findNccBasicSkuInfoData(dto);
        List<SkuInfoExportVo> collect = list.parallelStream().map(item -> {
            SkuInfoExportVo vo = new SkuInfoExportVo();
            BeanUtils.copyProperties(item, vo);
            return vo;
        }).collect(Collectors.toList());
        try {
            CsvExportUtils.exportToWeb(response, "商品信息.csv", collect);
            return AjaxResult.success("导出成功");
        } catch (IOException e) {
            e.printStackTrace();
            return AjaxResult.error("导出失败");
        }
    }

}
